//package com.qf.eduadmin.myinterceptor;

import com.alibaba.fastjson.JSON;
import com.qf.eduadmin.mapper.TbPermissionDao;
import com.qf.eduadmin.pojo.vo.LiveUser;
import com.qf.eduadmin.pojo.resp.ResponseData;
import com.qf.eduadmin.pojo.vo.TbPermission;
import com.qf.eduadmin.pojo.vo.TbUser;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author Mr.Liu
 * @date 2021/12/26 19:22
 */
/*
@Component
public class MyInterceptor implements HandlerInterceptor {
    @Resource
    private TbPermissionDao tbPermissionDao;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //判断用户是否登录
        TbUser tbuser = (TbUser) request.getSession().getAttribute("tbuser");
        if(tbuser!=null){
            //如果用户登录成功，在这里判断用户的权限
            List<TbPermission> permissions = tbPermissionDao.findPerByName(tbuser.getUserName());
            //将权限名放置在一个set集合中,进行权限的去重操作
            Set set = new HashSet();
            for(TbPermission tbPermission : permissions){
                set.add(tbPermission.getPermissionName());
            }
            //判断用户是否拥有该权限。因为我们的权限名称设置为接口路径，所以从request请求中可以获取到当前用户请求的url
            //根据查询出来的权限列表进行比对，如果通过，则用户拥有该权限，如果没有则代表没有该权限，返回提示
            String URI = request.getRequestURI();
            //如果拥有权限对象包含用户请求的该接口
            if(set.contains(URI)){
                //如果包含则代表该用户有访问该接口的权限，放行
                return true;
            }else {
                response.setCharacterEncoding("utf-8");
                response.setContentType("application/json");
                ResponseData responseData = new ResponseData(0,"用户暂无该权限",null,null);
                String s = JSON.toJSONString(responseData);
                response.getWriter().write(s);
                return false;
            }

        }
        //拦截请求，返回前端
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json");
        ResponseData responseData = new ResponseData(1,"未登录",null,null);
        String s = JSON.toJSONString(responseData);
        response.getWriter().write(s);
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
*/
